package com.mes.web.filter;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import com.mes.entity.tdd.Staff;
/**
 * 
* @ClassName: LoginFilter 
* @Description: 过滤器（过滤  .jsp 请求路径）
* @Author tdd
* @Date 2017-6-23 下午4:09:34 
* @version 1.0
 */
@Controller
public class LoginFilter implements Filter{
	public FilterConfig config;
	
	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		 // 获得在下面代码中要用的request,response,session对象
	       HttpServletRequest servletRequest = (HttpServletRequest) request;
	       HttpServletResponse servletResponse = (HttpServletResponse) response;
	       HttpSession session = servletRequest.getSession();
	
	       // 获得用户请求的URI
	        String path = servletRequest.getRequestURI();
	        //System.out.println(path);
       
	        // 从session里取员工工号信息
	        Staff staffSession = (Staff) session.getAttribute("staff");

	        /*创建类Constants.java，里面写的是无需过滤的页面
       		for (int i = 0; i < Constants.NoFilter_Pages.length; i++) {
			*/
	        //登录页面放行
          	if (path.indexOf("/login.jsp") > -1) {
          		chain.doFilter(servletRequest, servletResponse);
          		return;
          	}
          	if (path.indexOf("/pages/reLogin.jsp") > -1) {
          		chain.doFilter(servletRequest, servletResponse);
          		return;
          	}
          	if (path.indexOf("/reLogin.jsp") > -1) {
          		chain.doFilter(servletRequest, servletResponse);
          		return;
          	}
          	
	     
	        // 判断如果没有取到员工信息,就跳转到登陆页面
	        if (staffSession == null || "".equals(staffSession)) {
	          // 跳转到登陆页面
	        	servletResponse.sendRedirect("/toLogin.do");
		        } else {
	            // 已经登陆,继续此次请求
		            chain.doFilter(request, response);
		     }
		
	}
	
	public void init(FilterConfig config) throws ServletException {
	}
}
